package com.wangdao.wechatmall.controller.admin;

import com.wangdao.wechatmall.bean.bo.admin.BrandBO;
import com.wangdao.wechatmall.bean.bo.admin.BrandUpdateBO;
import com.wangdao.wechatmall.bean.bo.admin.CreateParamBO;
import com.wangdao.wechatmall.bean.pojo.Brand;
import com.wangdao.wechatmall.bean.vo.admin.BaseRespVo;
import com.wangdao.wechatmall.bean.vo.admin.BrandVO;
import com.wangdao.wechatmall.bean.vo.admin.CreateBrandVO;
import com.wangdao.wechatmall.exception.BrandDescException;
import com.wangdao.wechatmall.exception.BrandNameException;
import com.wangdao.wechatmall.exception.PriceException;
import com.wangdao.wechatmall.service.admin.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author liyongqiang
 * CreateDate 2021/7/9 10:53
 */

@RestController
@RequestMapping("admin/brand")
public class BrandController {

    @Autowired
    BrandService brandService;

    @RequestMapping("list")
    public BaseRespVo queryList(@Valid BrandBO brandBO, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return BaseRespVo.fail("品牌商id格式错误");

        }
        BrandVO brandVO = brandService.queryList(brandBO);
        return BaseRespVo.ok(brandVO);
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Brand brand){
        int delete = brandService.delete(brand.getId());
        if (delete == 1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody @Valid BrandUpdateBO brand, BindingResult bindingResult) throws BrandDescException, BrandNameException {
        if (bindingResult.hasErrors()){
            FieldError fieldError = bindingResult.getFieldError();
            assert fieldError != null;
            String defaultMessage = fieldError.getDefaultMessage();
            return BaseRespVo.fail(defaultMessage);

        }
        int update = brandService.update(brand);
        if (update == 1){
            return BaseRespVo.ok(brand);
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("create")
    public BaseRespVo create(@RequestBody @Valid CreateParamBO createParamBO, BindingResult bindingResult) throws PriceException, BrandDescException, BrandNameException {
        if (bindingResult.hasErrors()){
            FieldError fieldError = bindingResult.getFieldError();
            assert fieldError != null;
            String defaultMessage = fieldError.getDefaultMessage();
            return BaseRespVo.fail(defaultMessage);
        }
        CreateBrandVO createBrandVO = brandService.create(createParamBO);
        if (createBrandVO != null){
            return BaseRespVo.ok(createBrandVO);
        }
        return BaseRespVo.fail();
    }
}
